<?php
/**
 * 组装肤质检测的数据信息
 * Author: lyun
 * Date: 2018/4/9
 * Created by Panxsoft.
 */


namespace App\Services\Presenters;

use App\Models\OpenWechat\Authorizer;
use App\Models\Store\CamSkinRecord;
use App\Models\Store\HmSkinRecord;
use App\Models\Store\WaxSkinRecord;
use Illuminate\Database\Eloquent\Collection;

class SkinRecordPresenter
{

    /**
     * @param Collection $wax_skin_records
     * @return string
     */
    public static function assembleWaxSkinRecord(Collection $wax_skin_records)
    {
        $skin_data = '';

        $wax_skin_records->transform(function(WaxSkinRecord $wax_skin_record) use(&$skin_data){

            $skin_data .= (WaxSkinRecord::ITEM_LOCALE_MAP[trans('app.locale_language')][$wax_skin_record->itemsid]).
                '：'.(WaxSkinData::WAX_SKIN_DATA[trans('app.locale_language')][$wax_skin_record->itemsid][$wax_skin_record->levelsort]["levelname"]).'；';

            return $wax_skin_record;
        });

        return $skin_data;
    }

    /**
     * @deprecated (不要了！)
     * @param Collection $cam_skin_records
     * @return string
     */
    public static function assembleCamSkinRecord(Collection $cam_skin_records)
    {
        $skin_data = '';

        $cam_skin_records->transform(function(CamSkinRecord $cam_skin_record) use(&$skin_data){

            $skin_data = '皮肤健康：'.CamSkinData::CAM_SKIN_DATA['health'][self::assembleCamValueRange($cam_skin_record->health)]['title'].'；'.
                '黑眼圈：'.CamSkinData::CAM_SKIN_DATA['dark_circle'][self::assembleCamValueRange($cam_skin_record->dark_circle)]['title'].'；'.
                '青春痘：'.CamSkinData::CAM_SKIN_DATA['acne'][self::assembleCamValueRange($cam_skin_record->acne)]['title'].'；'.
                '色斑：'.CamSkinData::CAM_SKIN_DATA['stain'][self::assembleCamValueRange($cam_skin_record->stain)]['title'].'；';

            return $cam_skin_record;
        });

        return $skin_data;
    }

    /**
     * @param Collection $hm_skin_records
     * @return string
     */
    public static function assembleHmSkinRecord(Collection $hm_skin_records)
    {
        $skin_data = '';

        $hm_skin_records->transform(function(HmSkinRecord $hm_skin_record) use(&$skin_data){

            $skin_data .= HmSkinRecord::POSITIONS_LOCALE_MAP[trans('app.locale_language')][$hm_skin_record->position].
                trans('service.skin_age').$hm_skin_record->age.trans('service.age').'；';

            return $hm_skin_record;
        });

        return $skin_data;
    }

    /**
     * @param CamSkinRecord $cam_skin_record
     * @return array
     */
    public static function assembleDetailCamSkinRecord(CamSkinRecord $cam_skin_record)
    {
        return [
            'health_score'                => (int) $cam_skin_record->health,
            'health_beat_country_percent' => $cam_skin_record->getHealthBeatCountryPercent(),
            'health_string'               => CamSkinData::CAM_SKIN_DATA['health'][self::assembleHealthRange($cam_skin_record->health)],
            'dark_circle'                 => CamSkinData::CAM_SKIN_DATA['dark_circle'][self::assembleCamValueRange($cam_skin_record->dark_circle)],
            'acne'                        => CamSkinData::CAM_SKIN_DATA['acne'][self::assembleCamValueRange($cam_skin_record->acne)],
            'stain'                       => CamSkinData::CAM_SKIN_DATA['stain'][self::assembleCamValueRange($cam_skin_record->stain)],
            'created_at'                  => (string) $cam_skin_record->created_at,
            'updated_at'                  => (string) $cam_skin_record->updated_at,
        ];
    }

    /**
     * @param Collection $wax_skin_records
     * @param Collection $wax_suggests
     * @return array
     */
    public static function assembleDetailWaxSkinRecords(Collection $wax_skin_records, Collection $wax_suggests)
    {
        $base_wax_score = self::getBaseWaxScore($wax_skin_records);

        $itemsids = $wax_skin_records->pluck('itemsid');

        $diff_itemsids = collect(WaxSkinRecord::ITEMSID_ALL)->diff($itemsids);

        $wax_skin_records->transform(function(WaxSkinRecord $wax_skin_record) use($wax_suggests){

            return self::assembleDetailWaxSkinRecordByCustom($wax_skin_record, $wax_suggests);
        });

        $diff_itemsids->transform(function($diff_itemsid) use(&$wax_skin_records) {
            $wax_skin_records->prepend([
                'itemsid'     => $diff_itemsid,
                'itemsname'   => trans('admin.wax_itemsid_'.$diff_itemsid),
                'origin_img'  => null,
                'analyse_img' => null,
                'cause'       => null,
                'expert'      => null,
            ]);
        });

        $wax_skin_records = $wax_skin_records->sortBy('itemsid')->values();

        return ['base_wax_score' => $base_wax_score, 'wax_skin_records' => $wax_skin_records];
    }

    /**
     * @deprecated (废弃了，改用下面的：assembleDetailWaxSkinRecordByCustom)
     * @param WaxSkinRecord $wax_skin_record
     * @return WaxSkinRecord
     */
    public static function assembleDetailWaxSkinRecord(WaxSkinRecord $wax_skin_record): WaxSkinRecord
    {
        $wax_skin_record->itemsname = WaxSkinRecord::ITEM_LOCALE_MAP[trans('app.locale_language')][$wax_skin_record->itemsid];
        $wax_skin_record->cause = WaxSkinData::WAX_SKIN_DATA[trans('app.locale_language')][$wax_skin_record->itemsid][$wax_skin_record->levelsort]["levelintro"];
        $wax_skin_record->expert = WaxSkinData::WAX_SKIN_DATA[trans('app.locale_language')][$wax_skin_record->itemsid][$wax_skin_record->levelsort]["levelexperts"];
        $wax_skin_record->levelname = WaxSkinData::WAX_SKIN_DATA[trans('app.locale_language')][$wax_skin_record->itemsid][$wax_skin_record->levelsort]["levelname"];;

        return $wax_skin_record;
    }
    
    /**
     * @deprecated (废弃了，改用下面的：assembleDetailWaxSkinRecordByCustom)
     * @param WaxSkinRecord $wax_skin_record
     * @return WaxSkinRecord
     */
    public static function assembleMuwDetailWaxSkinRecord(WaxSkinRecord $wax_skin_record): WaxSkinRecord
    {
        $wax_skin_record->itemsname = WaxSkinRecord::ITEM_LOCALE_MAP[trans('app.locale_language')][$wax_skin_record->itemsid];
        $wax_skin_record->cause = MuwWaxSkinData::WAX_SKIN_DATA[trans('app.locale_language')][$wax_skin_record->itemsid][$wax_skin_record->levelsort]["levelintro"];
        $wax_skin_record->expert = MuwWaxSkinData::WAX_SKIN_DATA[trans('app.locale_language')][$wax_skin_record->itemsid][$wax_skin_record->levelsort]["levelexperts"];
        $wax_skin_record->levelname = MuwWaxSkinData::WAX_SKIN_DATA[trans('app.locale_language')][$wax_skin_record->itemsid][$wax_skin_record->levelsort]["levelname"];;
        
        return $wax_skin_record;
    }

    /**
     * @param WaxSkinRecord $wax_skin_record
     * @param Collection $wax_skin_suggests
     * @return WaxSkinRecord
     */
    public static function assembleDetailWaxSkinRecordByCustom(WaxSkinRecord $wax_skin_record, Collection $wax_skin_suggests): WaxSkinRecord
    {
        $suggest = $wax_skin_suggests->where('item_id', $wax_skin_record->itemsid)
            ->where('level_sort', $wax_skin_record->levelsort)
            ->first();

        $wax_skin_record->itemsname = trans('admin.wax_itemsid_'.$wax_skin_record->itemsid);
        $wax_skin_record->cause = $suggest->level_intro ?? '';
        $wax_skin_record->expert = $suggest->level_experts ?? '';
        $wax_skin_record->levelname = $suggest->level_name ?? '';

        return $wax_skin_record;
    }

    /**
     * @param Collection $hm_skin_records
     * @param Collection $hm_suggests
     * @return array
     */
    public static function assembleDetailHmSkinRecords(Collection $hm_skin_records, Collection $hm_suggests)
    {
        $hm_skin_record_positions = $hm_skin_records->pluck('position');

        $diff_positions = collect(HmSkinRecord::ALL_POSITIONS)->diff($hm_skin_record_positions);

        $hm_skin_records->transform(function(HmSkinRecord $hm_skin_record) use($hm_suggests){
            return self::assembleDetailHmSkinRecordByCustom($hm_skin_record, $hm_suggests);
        });

        $diff_positions->transform(function($diff_position) use(&$hm_skin_records) {
            $hm_skin_records->prepend([
                'skin_name' => trans('admin.hm_position_'.$diff_position),
                'position'  => $diff_position,
                "flex"      => null,
                "oil"       => null,
                "water"     => null,
                "age"       => null,
                "suggest"   => null,
            ]);
        });

        $hm_skin_records = $hm_skin_records->sortBy('position')->values();

        return $hm_skin_records->all();
    }

    /**
     * @deprecated (废弃了，改用下面的：assembleDetailHmSkinRecordByCustom)
     * @param HmSkinRecord $hm_skin_record
     * @return HmSkinRecord
     */
    public static function assembleDetailHmSkinRecord(HmSkinRecord $hm_skin_record): HmSkinRecord
    {
        $locale_report = HmSkinData::appLocaleReportMap();

        $hm_skin_record->skin_name = HmSkinRecord::POSITIONS_LOCALE_MAP[trans('app.locale_language')][$hm_skin_record->position];
        $hm_skin_record->evaluate = $locale_report['age'][self::hmAgeReportIndex($hm_skin_record->age)];

        $suggests = [
            $locale_report['water'][HmSkinData::waterLabelKey($hm_skin_record->water)],
            $locale_report['oil'][HmSkinData::oilLabelKey($hm_skin_record->oil)],
            $locale_report['flex'][HmSkinData::flexLabelKey($hm_skin_record->flex)],
        ];

        $hm_skin_record->suggests = $suggests;

        /*
         | 此处为 ios 需要显示的文字标签 ? 现有应该没有使用了吧
         */
        $hm_skin_record->labels = array_column($suggests, 'title');

        return $hm_skin_record;
    }

    /**
     * @param HmSkinRecord $hm_skin_record
     * @param Collection $hm_skin_suggests
     * @return HmSkinRecord
     */
    public static function assembleDetailHmSkinRecordByCustom(HmSkinRecord $hm_skin_record, Collection $hm_skin_suggests): HmSkinRecord
    {
        $locale_report = HmSkinData::appLocaleReportMap();

        $suggests = [];
        foreach(HmSkinData::HM_ITEM_MAP as $detection_type_id)
        {
            $hm_label_key = HmSkinData::HM_ITEM_NAME_MAP[$detection_type_id];

            $item_level_sort = HmSkinData::hmItemLevelSort($detection_type_id, $hm_skin_record->{$hm_label_key});
            $suggests[] = $hm_skin_suggests
                ->where('detection_type_id', $detection_type_id)
                ->where('level_sort', $item_level_sort)
                ->where('position_id', $hm_skin_record->position)
                ->first();
        }

        $hm_skin_record->skin_name = trans('admin.hm_position_' . $hm_skin_record->position);
        $hm_skin_record->evaluate = $locale_report['age'][self::hmAgeReportIndex($hm_skin_record->age)];
        $hm_skin_record->suggests = $suggests;

        /*
         | 此处为 ios 需要显示的文字标签 ? 现有应该没有使用了吧
         */
        $hm_skin_record->labels = array_column($suggests, 'title');

        return $hm_skin_record;
    }

    /**
     * @param $value
     * @return string
     */
    public static function assembleHealthRange($value)
    {
        if ($value <= 25) {
            $value = 3;
        } else if ($value <= 50) {
            $value = 2;
        } else if ($value <= 75) {
            $value = 1;
        } else {
            $value = 0;
        }

        return $value;
    }

    /**
     * @param $value
     * @return string
     */
    public static function assembleCamValueRange($value)
    {
        if ($value >= 61) {
            $value = 3;
        } else if ($value >= 41) {
            $value = 2;
        } else if ($value >= 21) {
            $value = 1;
        } else {
            $value = 0;
        }

        return $value;
    }

    /**
     * @param $value
     * @return int
     */
    public static function hmAgeReportIndex($value): int
    {
        if ($value >= 40) {
            $value = 4;
        } else if ($value >= 31) {
            $value = 3;
        } else if ($value >= 25) {
            $value = 2;
        } else if ($value >= 21) {
            $value = 1;
        } else {
            $value = 0;
        }

        return $value;
    }

    /**
     * @param Collection|WaxSkinRecord[] $wax_skin_records
     * @return array
     */
    public static function getBaseWaxScore(Collection $wax_skin_records)
    {
        $data = $wax_skin_records->pluck('value', 'itemsid')->all();

        $base_data = [];
        foreach(WaxSkinRecord::ITEMSID_ALL as $item_id)
        {
            $base_data[trans('admin.wax_itemsid_'.$item_id)] = $data[$item_id] ?? null;
        }

        return $base_data;
    }
}